﻿@model Marbale.BusinessObject.Tax.MasterTax
@{
    ViewBag.Title = "Tax";
}
<div>
    <div class="taxsetmodel">
        <div id="taxvalidation" style="text-align: center; display: none">
            <p style="font-weight: 400; color: red">No numbers above 100</p>
        </div>
        <div id="taxtable">
            <table id="marbleTable" class="table marbleTable">
                <thead>
                    <th>ID</th>
                    <th>Tax Name</th>
                    <th>Tax Percentage</th>
                    <th>Active Flag</th>
                </thead>
                @if (Model.Taxset != null)
                {
                    int count = 1;
                    foreach (var item1 in Model.Taxset)
                    {  
           
                    <tr onchange="UpdatedList(this)">
                        <td onclick="Getvalues(@item1.TaxId)">
                            <input type="text" id="TaxId@(count)" value="@item1.TaxId" disabled >
                        </td>
                        <td onclick="Getvalues(@item1.TaxId)">
                            <input type="text" value="@item1.TaxId"  id="TaxName@(count)"/>
                        </td>
                        <td onclick="Getvalues(@item1.TaxId)">
                            <input type="number" value="@item1.TaxPercent" class="numberbox" id="TaxPercent@(count)" />
                        </td>
                        <td onclick="Getvalues(@item1.TaxId)">
                            <input type="checkbox"  @(item1.ActiveFlag ? "checked='checked'" : "") value="@item1.ActiveFlag"  id="ActiveFlag@(count)"/>
                        </td>
                    </tr> 
                        count++;
                    }
                }
            </table>
        </div>
        <div class="action">
            <div style="float:right;margin-right:20px">
                <button class="btn btn-primary btn-md" role="button" onclick="SaveTax()">Save</button>
                <button class="btn btn-primary btn-md" role="button" onclick="history.back();">Back</button>
                <button class="btn btn-primary btn-md" role="button" onclick="AddProduct()">Add</button>
                <button class="btn btn-primary btn-md" role="button" onclick="location.href='/'">Close</button>
            </div>
        </div>
    </div>
    <br />
    <br />
    <div id="taxstructure">
    </div>
</div>
<script>

    $('.numberbox').keyup(function () {
        if ($(this).val() > 100) {
            $('#taxvalidation').show();
            $(this).val('0');
        }
        else {
            $('#taxvalidation').hide();
        }
    });
    $("#marbleTable tr").click(function () {
        var selected = $(this).hasClass("highlight");
        $("#marbleTable tr").removeClass("highlight");
        if (!selected)
            $(this).addClass("highlight");
    });

    var GetvaluesList = [];
    function Getvalues(x) {

        GetvaluesList = [];
        GetvaluesList.push(x);
        localStorage['TaxId'] = GetvaluesList[0];
        $('#taxstructure').load('/Tax/GetTaxStructure', { TaxId: GetvaluesList[0] });
    }


    function AddProduct() {
        var index = $("#marbleTable tbody tr").length + 1;
        var TaxId = "TaxId" + index;
        var TaxId = "TaxName" + index;
        var TaxPercent = "TaxPercent" + index;
        var ActiveFlag = "ActiveFlag" + index;
        var newRow = '<tr onchange=UpdatedList(this)> <td> <input type="text" disabled ></td>' +
     '<td> <input type="text" id="' + TaxId + '" />  </td>' +
     '<td> <input type="number"  id="' + TaxPercent + '" />  </td>' +
     '<td> <input type="checkbox" id="' + ActiveFlag + '"/></td>' + '</tr>';
        $('#marbleTable tr:last').after(newRow);
    }
    function UpdatedList(x) {
        updatedArray = [];
        updatedArray.push(x.rowIndex);
    }
    function SaveTax() {
        var table = document.getElementById('marbleTable');
        var id = updatedArray[0];
        var TaxId = $('#TaxName' + id).val();
        var TaxPercent = $('#TaxPercent' + id).val();
        var ActiveFlag = $('#ActiveFlag' + id).val();
        var taxmaster = {
            TaxId: $('#TaxId' + id).val(),
            TaxId: $('#TaxName' + id).val(),
            TaxPercent: $('#TaxPercent' + id).val(),
            ActiveFlag: $("#ActiveFlag" + id).is(':checked')
        };
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: '@Url.Action("InsertUpdateTax", "Tax")',
            data: JSON.stringify({ taxmaster: taxmaster }),
            dataType: "json",
            success: function (data) {
                alert('Data Saved');
                location.reload();
            },
            error: function (e) {
                alert(e);
            }
        });
    }


</script>

